
package com.weishu.file.controller;

import com.weishu.common.core.utils.IdUtils;
import com.weishu.file.config.MsgConfig;
import com.weishu.file.service.AliyunSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;


@RestController
@RequestMapping("/sms/")
public class AliyunSmsController {

	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private AliyunSmsService aliyunSmsService;

	@Autowired
	private AmqpTemplate amqpTemplate;


	/**
     * 验证verifyCode
     * @param phoneNum
     * @param verifyCode
     * @return
     */
	@GetMapping(value="checkVerifyCode")
	public Boolean checkVerifyCode(@RequestParam("phoneNum") String phoneNum, @RequestParam("verifyCode") String verifyCode) {
		return aliyunSmsService.checkVerifyCode(phoneNum, verifyCode);
	}
    /**
     * 发送短信验证码
     * 每日最多获取3条验证码
     * @param phoneNum
     * @return
     */
	@GetMapping(value="sendVerifyCode")
	public Boolean sendVerifyCode(@RequestParam("phoneNum") String phoneNum) {
		HashMap message = new HashMap();
		message.put("id", IdUtils.fastSimpleUUID());
		message.put("phoneNum", phoneNum);
		amqpTemplate.convertAndSend(MsgConfig.msgExchange, MsgConfig.msgRoutingKey, message);
		logger.info("send exchange:{},routingKey:{},message:{}",MsgConfig.msgExchange, MsgConfig.msgRoutingKey, message);
		return true;
	}
}
